package CricleQueue

import (
	"errors"
	"fmt"
)

const QueueSize = 6

// 环形队列
type CricleQueue struct {
	dataSoure [QueueSize]interface{} //数组
	front     int                    //头部的位置
	tail      int                    //尾部的位置
}

func NewCricleQueue() *CricleQueue {
	cq := new(CricleQueue)
	cq.front = 0
	cq.tail = 0

	return cq
}

// 环形大小
func (cq *CricleQueue) QueueLength() int {
	return (cq.tail - cq.front + QueueSize) % QueueSize
}

// 入队
func (cq *CricleQueue) EnQueue(data interface{}) error {
	if (cq.tail+1)%QueueSize == cq.front%QueueSize {
		fmt.Println("队列已满")
		return errors.New("队列已满")
	}

	cq.dataSoure[cq.tail] = data
	cq.tail = (cq.tail + 1) % QueueSize
	return nil
}

// 取出
func (cq *CricleQueue) DeQueue() interface{} {
	if cq.front == cq.tail {
		return errors.New("队列已空")
	}
	data := cq.dataSoure[cq.front]

	cq.front = (cq.front + 1) % QueueSize
	return data

}

// 删除
func (cq *CricleQueue) DelQueue(data interface{}) error {

	if cq.front == cq.tail {
		return errors.New("队列已空")
	}

	index := 0
	for i := 0; i < QueueSize; i++ {
		if cq.dataSoure[i] == data {
			index = i
		}
	}

	if index != 0 {
		for i := index; i < QueueSize-1; i++ {
			cq.dataSoure[i] = cq.dataSoure[i+1]
		}
	}
	cq.tail--

	return nil

}

// 是否为空
func (cq *CricleQueue) IsEmpty() bool {
	return cq.front == cq.tail
}
